import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  {
    path: '/login',
    name: 'login',
    component: () => import('../pages/login/login.vue')
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('../pages/home/index.vue'),
    redirect: '/welcome',
    children: [
      { path: '/welcome', name: 'welcome', component: () => import('../pages/home/welcome.vue') },
      { path: '/users', name: 'users', component: () => import('../pages/userManage/index.vue') },
      { path: '/roles', name: 'roles', component: () => import('../pages/authManage/roles.vue') },
      { path: '/rights', name: 'authority', component: () => import('../pages/authManage/authority.vue') },
      { path: '/categories', name: 'categories', component: () => import('../pages/goodsManage/cate.vue') },
      { path: '/params', name: 'params', component: () => import('../pages/goodsManage/params.vue') },
      { path: '/goods', name: 'goods', component: () => import('../pages/goodsManage/goodsList.vue') },
      { path: '/goods/add', name: 'add', component: () => import('../pages/goodsManage/add.vue') },
      { path: '/orders', name: 'orders', component: () => import('../pages/orderManage/orderList.vue') },
      { path: '/reports', component: () => import('../pages/reportManage/reportList.vue') }
      // ,
      // {
      //   path: '/aasdasd',
      //   name: 'aasdasd',
      //   meta: {
      //     isAuth: true
      //   },
      //   component: () => import('/sad/asd/asd.vue')
      // }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to   将要访问的路径
  // from 从那个路径来
  // next 是一个表示 放行 的函数
  //     next() 放行   next('/login')   强制跳转
  if (to.path === '/login') return next()
  // 获取 token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
/**
 * 需要在路由中增加一下部分
 * meta: {
 *    isAuth: true   // 需要鉴权
 * },
 * 
 */
// router.beforeEach((to, from, next) => {
//   if (to.path == '/login') {  
//     next()
//   } else if (to.meta.isAuth) {
//     if (window.sessionStorage.getItem('token')) { // 如果有 token 就进入该页面
//       next()
//     } else { // 没有 token 进入登录页
//       next('/login')
//     }
//   } else { // 不要登录 直接进入
//     next()
//   }
// })

export default router
